/*
 *                    COPYRIGHT NOTICE
 *
 *           This code is copyright protected by Freedom Inc.
 *
 *              2001-2012 All Rights Are Reserved
 */
package com.freedom.oldsong.system.service.impl;

import org.apache.log4j.Logger;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.freedom.oldsong.system.dao.UserDao;
import com.freedom.oldsong.system.model.User;
import com.freedom.oldsong.system.service.UserHolder;

/**
 * @author steven.wang
 *
 */
public class UserHolderImpl implements UserHolder {
    private Logger logger = Logger.getLogger(UserHolderImpl.class);
	
	private UserDao userDao;
	
	/**
	 * {@inheritDoc}
	 */
	public final User getCurrentUser() {
		Object obj = getPrincipal();
		String username;
		if (obj instanceof UserDetails) {
			username = ((UserDetails)obj).getUsername();
		} else {
			username = obj.toString();
		}
		if(logger.isDebugEnabled()){
		    logger.debug("username=" + username);
		}
		return userDao.getUserByLoginName(username);
	}

	protected Object getPrincipal() {
		return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	

}
